#import "AmapFlutterSearchPlugin.h"
#import <AMapSearchKit/AMapSearchKit.h>


@interface AmapFlutterSearchPlugin ()<AMapSearchDelegate>
{
    AMapSearchAPI *_searchApi;
    FlutterResult _resultCallback;
}

@end


@implementation AmapFlutterSearchPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"amap_flutter_search"
            binaryMessenger:[registrar messenger]];
  AmapFlutterSearchPlugin* instance = [[AmapFlutterSearchPlugin alloc] init];
  [registrar addMethodCallDelegate:instance channel:channel];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
    
    
    if([@"updatePrivacy" isEqualToString:call.method]){
        
        NSString *apiKey = [NSString stringWithFormat:@"%@",call.arguments];
        [AMapSearchAPI updatePrivacyShow:AMapPrivacyShowStatusDidShow privacyInfo:AMapPrivacyInfoStatusDidContain];
        [AMapSearchAPI updatePrivacyAgree:AMapPrivacyAgreeStatusDidAgree];
        //配置用户Key
        [AMapServices sharedServices].apiKey = apiKey;

        result(@"1");
    } else if([@"searchPoi" isEqualToString:call.method]){
        
        NSString *keyword = call.arguments[@"keyword"];
        NSString *city = call.arguments[@"city"];
        NSString *page = call.arguments[@"page"];
        NSString *latitude = call.arguments[@"latitude"];
        NSString *longitude = call.arguments[@"longitude"];
        NSString *types = call.arguments[@"types"];

        _searchApi = [[AMapSearchAPI alloc] init];
        _searchApi.delegate = self;
    
        AMapPOIKeywordsSearchRequest *searchRequest = [[AMapPOIKeywordsSearchRequest alloc] init];
         searchRequest.keywords = keyword;
        searchRequest.types = types ? : @"";
        searchRequest.city = city;
        searchRequest.offset = 25;
        searchRequest.page = [page intValue];
        searchRequest.showFieldsType = AMapPOISearchShowFieldsTypeAll;
        /*  搜索SDK 3.2.0 中新增加的功能，只搜索本城市的POI。*/
        searchRequest.cityLimit = YES;
        AMapGeoPoint *center = [[AMapGeoPoint alloc] init];
        center.latitude = [latitude doubleValue];
        center.longitude =  [longitude doubleValue];
        searchRequest.location = center;
        
        [_searchApi AMapPOIKeywordsSearch:searchRequest];
        _resultCallback = result;
        
    }  
    else if ([@"getPlatformVersion" isEqualToString:call.method]) {
    result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
  }
  
  else {
    result(FlutterMethodNotImplemented);
  }
    
/*
 1.注册高德.
 2.搜素当前城市的最近位置
 */
}


///!!!mark - 搜索的回调代理
/**
 * @brief 当请求发生错误时，会调用代理的此方法.
 * @param request 发生错误的请求.
 * @param error   返回的错误.
 */
- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error{
    
    NSLog(@"搜索错误的数据");
    _resultCallback(@[]);
    
}

/**
 * @brief POI查询回调函数
 * @param request  发起的请求，具体字段参考 AMapPOISearchBaseRequest 及其子类。
 * @param response 响应结果，具体字段参考 AMapPOISearchResponse 。
 */
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response{
    
    ///关键字建议列表和城市建议列表
    ///POI全局唯一ID
    ///名称
//    @property (nonatomic, copy)   NSString     *name;
//    ///兴趣点类型
//    @property (nonatomic, copy)   NSString     *type;
//    ///类型编码
//    @property (nonatomic, copy)   NSString     *typecode;
//    ///经纬度
//    @property (nonatomic, copy)   AMapGeoPoint *location;
//    ///地址
//    @property (nonatomic, copy)   NSString     *address;
//    ///电话
//    @property (nonatomic, copy)   NSString     *tel;
//    ///距中心点的距离，单位米。在周边搜索时有效
//    @property (nonatomic, assign) NSInteger     distance __attribute__((deprecated("已废弃, from 9.4.0")));
//    ///停车场类型，地上、地下、路边
//    @property (nonatomic, copy)   NSString     *parkingType;
//    ///商铺id
//    @property (nonatomic, copy)   NSString     *shopID;
//
//    ///邮编
//    @property (nonatomic, copy)   NSString     *postcode;
//    ///网址
//    @property (nonatomic, copy)   NSString     *website;
//    ///电子邮件
//    @property (nonatomic, copy)   NSString     *email;
//    ///省
//    @property (nonatomic, copy)   NSString     *province;
//    ///省编码
//    @property (nonatomic, copy)   NSString     *pcode;
//    ///城市名称
//    @property (nonatomic, copy)   NSString     *city;
//    ///城市编码
//    @property (nonatomic, copy)   NSString     *citycode;
//    ///区域名称
//    @property (nonatomic, copy)   NSString     *district;
//    ///区域编码
//    @property (nonatomic, copy)   NSString     *adcode;
//    ///POI对应的导航引导点坐标 @since 9.4.0
//    @property (nonatomic, copy)   NSString     *naviPOIId;
//    ///地理格ID
//    @property (nonatomic, copy)   NSString     *gridcode;
//    ///入口经纬度
//    @property (nonatomic, copy)   AMapGeoPoint *enterLocation;
//    ///出口经纬度
//    @property (nonatomic, copy)   AMapGeoPoint *exitLocation;
//    ///方向
//    @property (nonatomic, copy)   NSString     *direction;
//    ///是否有室内地图
//    @property (nonatomic, assign) BOOL          hasIndoorMap;
//    ///所在商圈
//    @property (nonatomic, copy)   NSString     *businessArea;
//    ///室内信息
//    @property (nonatomic, strong) AMapIndoorData *indoorData;
//    ///子POI列表
//    @property (nonatomic, strong) NSArray<AMapSubPOI *> *subPOIs;
//    ///图片列表
//    @property (nonatomic, strong) NSArray<AMapImage *> *images;
//    ///所在商圈 @since 9.4.0
//    @property (nonatomic, strong) AMapBusinessData *businessData;
//    ///扩展信息只有在ID查询时有效
//    @property (nonatomic, strong) AMapPOIExtension *extensionInfo;
//    @end
    
    NSArray<AMapPOI *> *pois = response.pois;
    NSMutableArray *dataList = [[NSMutableArray alloc] init];
    if (pois.count == 0){
        _resultCallback(@[]);
    } else {
        
        for (AMapPOI *element in pois) {
            
            
            AMapGeoPoint *location = element.location;
            // 处理元素
            [dataList addObject:@{@"uid":element.uid,
                                  @"name":element.name,
                                  @"latitude":[NSString stringWithFormat:@"%f",location.latitude],
                                  @"longitude":[NSString stringWithFormat:@"%f",location.longitude],
                                  @"address":element.address,
                                  @"tel":element.tel,
                                  @"province":element.province,
                                  @"pcode":element.pcode,
                                  @"city":element.city,
                                  @"citycode":element.citycode,
                                  @"district":element.district,
                                  @"adcode":element.adcode,
                                }];
        }
        
        NSLog(@"搜索信息%@",dataList);
        _resultCallback(dataList);
    }
}



@end
